package com.wyp.didishige.activity;

import android.annotation.SuppressLint;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wyp.didishige.activity.login.LoginActivity;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.R;

import cn.bmob.v3.BmobUser;

/**
 * 引导页
 *
 * @author smile
 * @ClassName: SplashActivity
 * @Description: TODO
 * @date 2014-6-4 上午9:45:43
 */
public class SplashActivity extends BaseActivity {

    private static final int GO_HOME = 100;
    private static final int GO_LOGIN = 200;
    private TextView mVersion;
    private View mTopLay;
    Animation mTopIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置程序不显示标题
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置不显示状态栏
        super.onCreate(savedInstanceState);
        hideActionBar();
        setContentView(R.layout.activity_splash);
        mTopIn = AnimationUtils.loadAnimation(this, R.anim.hr_dialog_bottom_enter_anim);
        MobclickAgent.updateOnlineConfig(this);
        init();
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTopLay = findViewById(R.id.mTopLay);
        mVersion= (TextView) findViewById(R.id.mVersion);

        PackageManager packageManager=getPackageManager();

        try {
            PackageInfo packageInfo=packageManager.getPackageInfo(this.getPackageName(),0);
            mVersion.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        mTopLay.startAnimation(mTopIn);
    }
    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        mTopIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mTopLay.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//        mHandler.sendEmptyMessageDelayed(GO_HOME, 2000);
        if (BmobUser.getCurrentUser(this, User.class) != null) {
            mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
        } else {
            mHandler.sendEmptyMessageDelayed(GO_LOGIN, 3000);
        }
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    IntentUtil.start_activity(SplashActivity.this, MainActivity.class);
                    finish();
                    break;
                case GO_LOGIN:
                    IntentUtil.start_activity(SplashActivity.this, LoginActivity.class);
                    finish();
                    break;
            }
        }
    };

}
